package com.haiya.observer.bellcase;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RingEventSource {
    private List<BellEventListener> listeners;

    public RingEventSource() {
        listeners = new ArrayList<>();
    }

    public void addListener(BellEventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(BellEventListener listener) {
        listeners.remove(listener);
    }

    /**
     * @param sound: 上课、下课
     */
    public void ring(String sound) {
        String type = sound + "铃声";
        System.out.println(type + "想起~~~~~~");
        RingEvent event = new RingEvent(this, sound);
        notifyEvent(event);
    }

    public void notifyEvent(RingEvent event) {
        BellEventListener eventListener = null;
        Iterator<BellEventListener> iterator = listeners.iterator();
        while(iterator.hasNext()) {
            eventListener = iterator.next();
            eventListener.heardBell(event);
        }
    }
}